#include "process.h"
#include <unistd.h>
#include <string.h>

#define SIZE 101
#define STYLE '='


void FlushProcess(double total, double curr)
{
  if(curr > total) //防出错
      curr = total;

  double rate = (curr / total) * 100; 
  int cnt = (int)rate; //38.8 ->38
  char Processbuff[SIZE];
  memset(Processbuff, '\0', sizeof(Processbuff));
  int i = 0;
  for(; i < cnt; i++)
    Processbuff[i] = STYLE;

  static const char *lable = "|/-\\";
  static int index = 0;
  //刷新
  printf("[%-100s] [%.1lf%%][%c]\r",Processbuff, rate, lable[index++]);
  index %= strlen(lable);
  fflush(stdout);
}



//version1:只能说明原理，实际上不是这样使用
void Process()
{
  const char *lable = "|/-\\";
  int len = strlen(lable);
  char Processbuff[SIZE];
  memset(Processbuff, '\0', sizeof(Processbuff));
  int cnt = 0;

  while(cnt <= 100)
  {
    printf("[%-100s] [%d%%] [%c]\r", Processbuff, cnt, lable[cnt%len]);
    fflush(stdout);
    Processbuff[cnt++] = STYLE;
    usleep(30000);
  }
  printf("\n");
}
